﻿

using System;
using System.Diagnostics;
using FakeItEasy;
using Fluent.IO;
using FluentAssertions;
using Wolfpack.Contrib.Deployment;
using Wolfpack.Contrib.Deployment.Exe;
using Wolfpack.Core.Interfaces.Entities;

namespace Wolfpack.Contrib.Tests.Publishers.Deployment
{
    public class ExecuteDeploymentPublisherDomain : DeploymentPublisherDomainBase<ExecuteDeploymentConfig>
    {
        protected readonly IProcessLauncher _launcher;

        private int _launcherResult;
        private ProcessStartInfo _actualLauncherProcessArg;

        private class TestDeploymentPublisher : ExecuteDeploymentPublisherBase<ExecuteDeploymentConfig>
        {
            public TestDeploymentPublisher(ExecuteDeploymentConfig config)
                : base(config)
            {                
            }

            protected override PluginDescriptor BuildMessageIdentity()
            {
                return new PluginDescriptor
                           {
                               Description = "TestPublisher",
                               Name = Config.FriendlyId,
                               TypeId = new Guid("F231DF2B-253A-451F-844D-DDF26A7BEE52")
                           };
            }
        }

        public ExecuteDeploymentPublisherDomain()
        {
            SetDeploymentBaseConfig(new ExecuteDeploymentConfig());
            _launcher = A.Fake<IProcessLauncher>();
        }

        public void TheCommandLine_(string cmdline)
        {
            _config.Command = cmdline;
        }

        public void TheLaunchCommandLinePathShouldBeRooted()
        {
            Path.Get(_actualLauncherProcessArg.FileName).IsRooted.Should().BeTrue();
        }

        public void TheLaunchCommandLinePathShouldEndWith_(string expected)
        {
            _actualLauncherProcessArg.FileName.EndsWith(expected).Should().BeTrue();
        }

        public void TheLauncherIndicatesSuccess()
        {
            _launcherResult = 0;
        }

        public void TheLaunchCommandLinePathShould_(string expected)
        {
            _actualLauncherProcessArg.FileName.Should().Be(expected);
        }

        public void TheLauncherReturnsErrorCode_(int errorCode)
        {
            _launcherResult = errorCode;
        }

        public void ThePublisherIsCalled()
        {
            A.CallTo(() => _launcher.Launch(A<ProcessStartInfo>.Ignored))
                .Invokes(call =>
                             {
                                 _actualLauncherProcessArg = (ProcessStartInfo)call.Arguments[0];
                             })
                .Returns(_launcherResult);

            var sut = new TestDeploymentPublisher(_config)
            {
                Launcher = _launcher,
                Updater = _updater
            };

            sut.Consume(_triggerNotification);
        }

        public void TheLauncherWasCalled()
        {
            _actualLauncherProcessArg.Should().NotBeNull();
        }
    }
}